package com.waiting.leetcode;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/5
 **/
public class SumOfN {
    public static void main(String[] args) {
        p(10);
    }

    static void print(int[] r, int len) {
        for (int i = 0; i <= len; i++) {
            System.out.print(r[i] + " ");
        }
        System.out.println();
    }

    public static void p(int n) {
        int sum = 0, len = 0;
        while (sum < n) {
            sum += len;
            len++;
        }
        System.out.println(len);
        len--;
        int r[] = new int[len];
        for (int i = 0; i < len; i++) {
            r[i] = i + 1;
        }
        int k = len - 2;
        sum -= r[len - 1];
        while (true) {
            if (k == 0 && r[k] == n) {
                break;
            }
            if (sum < n) {
                r[k + 1] = r[k] + 1;
                k++;
                sum += r[k];
            } else {
                if (sum == n) {
                    print(r, k);
                }
                sum -= r[k];
                r[--k]++;
                sum++;
            }
        }
    }
}
